import django_filters
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from tenant import models
from tenant.ser import Tenant,TenType
from django_filters import FilterSet
# Create your views here.
from rest_framework.pagination import PageNumberPagination
class Page(PageNumberPagination):
    page_size=2
    page_size_query_param='size'
class Filters(FilterSet):
    name=django_filters.CharFilter(field_name='name',lookup_expr='contains')
    class Meta:
        model=models.Tenant
        fields=['type','status']
class TenTypeView(ModelViewSet):
    queryset=models.TenType.objects.all()
    serializer_class=TenType
class TenantView(ModelViewSet):
    queryset=models.Tenant.objects.all()
    serializer_class=Tenant
    pagination_class = Page
    filter_backends = [django_filters.rest_framework.DjangoFilterBackend]
    filterset_class=Filters
class UpdateStatus(APIView):
    def get(self,request):
        id=request.query_params.get('id')
        try:
            tenant=models.Tenant.objects.get(id=id)

        except:
            return Response({'msg':'no'})
        tenant.status=not tenant.status
        tenant.save()
        ser=Tenant(tenant)
        return Response({"msg":"ok","data":ser.data})
